<html>
<head>
<script src="../../resources/js-test.js"></script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="resources/canvas-context-lost-events.js"></script>
</head>
<body>
<script>
var ctx;
var lostEventHasFired = false;
var contextLostTest;
async_test(t => {
    if (window.internals) {
        var canvas = new OffscreenCanvas(500, 500);
        canvas.addEventListener('contextlost', contextLost);
        canvas.addEventListener('contextrestored', contextRestored);
        ctx = canvas.getContext('2d');

        ctx.fillRect(0, 0, 1, 1);
        // setTimeout creates a frame barrier that locks the canvas into gpu
        // acceleration mode when running under virtual/gpu
        setTimeout(() => {
            // Now it is safe to use verifyContextLost without fearing side-effects
            // because a rendering mode was fixed.
            verifyContextLost(false, ctx);

            internals.forceLoseCanvasContext(canvas, "2d");
            if (!ctx.isContextLost()) {
                assert_true(false, "canvas context is not lost properly.");
                t.done();
            } else {
                verifyContextLost(true, ctx);
            }
        }, 0);
        t.done();
    } else {
        assert_true(false, "This test requires window.internals.");
        t.done();
    }
}, "Test the behavior of disconneced canvas recovery after a gpu context loss");

function contextLost() {
    contextLost(lostEventHasFired, ctx);
}

function contextRestored() {
    contextRestored(lostEventHasFired, ctx);
}

</script>
</body>
</html>
